// pages/groupActDetail/groupActDetail.js
const app = getApp();
var WxParse = require('../../../wxParse/wxParse.js');
import api from '../../../utils/util.js';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    discount: app.globalData.discount,
    ent: app.globalData.ent,
    navmess: {
      navtitle: "拼团活动详情",
      backhide: 0,
      topheight: 1,
    },
    swiperSetting:{
      indicatorDots: true,
      vertical: false,
      autoplay: false,
      interval: 2000,
      duration: 500,
    },

    // 倒计时
    endTime: "",
    demolist:[]
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 倒计时
  countDown:function(end){
    var that=this;
    var nowTime = new Date().getTime();//现在时间（时间戳）
    var endTime = new Date(that.data.endTime).getTime();//结束时间（时间戳）
    var time = (endTime-nowTime)/1000;//距离结束的毫秒数
    // 获取天、时、分、秒
    let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    day = that.timeFormin(day),
    hou = that.timeFormin(hou),
    min = that.timeFormin(min),
    sec = that.timeFormin(sec)
    that.setData({
      day: that.timeFormat(day),
      hou: that.timeFormat(hou),
      min: that.timeFormat(min),
      sec: that.timeFormat(sec)
    })
    //每1000ms刷新一次
    if (time>0){
      that.setData({
        countDown: true
      })
      setTimeout(this.countDown, 1000);
    }else{
      that.setData({
        countDown:false
      })
    }
  },
  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数（不会出现负数）
  timeFormin(param) {
    return param < 0 ? 0: param;
  },
  //单独购买
  actToCart: function(e){
    var that = this;
    var params = {
      "token": that.data.token,
      "enterpriseNo": that.data.ent,
      "goodsType": that.data.demolist.goodsType,
      "goodsId": that.data.demolist.goodsId,
      "quantity": 1
    }
    console.log(params)
    api.post("/mini/my/cart/joinCart",params).then(res =>{
      if (res.code == 200) {
          console.log(res)
          wx.navigateTo({
            url: e.currentTarget.dataset.url+ "?type=1"
          })
      }
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
  },
  gotoDetail2: function(e){
    var that = this,
        mess = e.currentTarget.dataset.mess,
        url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url+ "?mess=" +mess,
    })
  },
  //发起拼团
  launchGroup: function(e){
    var that = this,
        mess = this.data.activityNo,
        url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url+ "?mess=" +mess,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
        check = app.checkLogin();
    if(check){
      var token = wx.getStorageSync('token');
      this.setData({
        isLogin: check,
        token: token
      })
    }
    console.log("activityNo: ",JSON.parse(options.mess))
    that.setData({
      activityNo: JSON.parse(options.mess)
    })
    api.post("/mini/ext/spellGroup/info",{
      "activityNo": JSON.parse(options.mess)
    }).then(res => {
      console.log("拼团信息",res)
      that.setData({
        demolist: res.result,
        endTime: res.result.endTime,
        imageUrl: res.result.imageUrl.split(",")
      })
      that.countDown();
      // 富文本解析初始化数据
      WxParse.emojisInit('[]', "../../../wxParse/emojis/", {
        "00": "00.gif",
        "01": "01.gif",
        "02": "02.gif",
        "03": "03.gif",
        "04": "04.gif",
        "05": "05.gif",
        "06": "06.gif",
        "07": "07.gif",
        "08": "08.gif",
        "09": "09.gif",
        "09": "09.gif",
        "10": "10.gif",
        "11": "11.gif",
        "12": "12.gif",
        "13": "13.gif",
        "14": "14.gif",
        "15": "15.gif",
        "16": "16.gif",
        "17": "17.gif",
        "18": "18.gif",
        "19": "19.gif",
      });
      var article = res.result.detail;
      /*** WxParse.wxParse(bindName , type, data, target,imagePadding)
      * 1.bindName绑定的数据名(必填)
      * 2.type可以为html或者md(必填)
      * 3.data为传入的具体数据(必填)
      * 4.target为Page对象,一般为this(必填)
      * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)*/ 
      var temp = WxParse.wxParse('article', 'html', article, that, 5);
    })
    // 未满拼团列表
    api.post("/mini/ext/spellGroup/unFullSpellGroup",{
      "ent": app.globalData.ent,
      "activityNo": JSON.parse(options.mess),
      "pageNum": 1,
      "pageSize": 2
    }).then(res => {
      console.log("未满拼团",res)
      that.setData({
        notfulllist: res.result.list
      })
      setInterval(function(){
        that.setData({
          beginTime: JSON.stringify(new Date())
        })
        // console.log(typeof that.data.beginTime)
      },1000)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})